#!/bin/bash
LOCKET_DIR=INSTALL_DIR
export PATH=$LOCKET_DIR/bin:$LOCKET_DIR/dbs/bin:$PATH
export LD_LIBRARY_PATH=$LOCKET_DIR/lib:$LOCKET_DIR/libexec:$LD_LIBRARY_PATH
#日志库zlog自诊断日志文件,屏蔽debug日志
#export ZLOG_PROFILE_DEBUG=$LOCKET_DIR/dbs/log/zlog.debug.log
export ZLOG_PROFILE_ERROR=$LOCKET_DIR/dbs/var/log/zlog.error.log
SQLR_LISTENER=sqlr-listener
SQLR_CONNECTION=sqlr-connection
SQLR_SCALER=sqlr-scaler
SQLR_START=sqlr-start
SQLR_STOP=sqlr-stop
DATASYNC=lkt-datasync
SQLR_TOTAL=sqlr-

ipc_clean (){
    #清理环境 (若dbs非正常退出, 有些资源不会主动释放，从而导致下次启动会出现启动失败的问题)
    #清理数据同步进程的IPC资源
    ipcrm -M 1987 2> /dev/null 
    rm -fr $LOCKET_DIR/dbs/var/run/dbs-datasync.pid 
    #清理SQLRelay的IPC资源
    SQLRELAY_IPC_ID=`$LOCKET_DIR/dbs/bin/ftok.sh $LOCKET_DIR/var/run/sqlrelay/mysql.ipc 1` #shell计算ftok
    ipcrm -M $SQLRELAY_IPC_ID 2>/dev/null
    ipcrm -S $SQLRELAY_IPC_ID 2>/dev/null
    rm  -fr $LOCKET_DIR/var/run/sqlrelay/* #清理ipc,socket资源

}
status (){
    SQLR_LISTENER_NUM=`ps -fe| grep $SQLR_LISTENER | grep -v grep|wc -l` 
    if [ $SQLR_LISTENER_NUM -eq 0 ];then
        STATUS="\033[31m[FAILED]\033[0m"
    else
        STATUS="\033[32m[SUCCESS]\033[0m"
    fi
    echo -e "$SQLR_LISTENER \t\t $SQLR_LISTENER_NUM   $STATUS"

    SQLR_CONNECTION_NUM=`ps -fe| grep $SQLR_CONNECTION | grep -v grep | wc -l`
    if [ $SQLR_CONNECTION_NUM -eq 0 ];then
        STATUS="\033[31m[FAILED]\033[0m"
    else
        STATUS="\033[32m[SUCCESS]\033[0m"
    fi
    echo -e "$SQLR_CONNECTION \t $SQLR_CONNECTION_NUM   $STATUS"

    SQLR_SCALER_NUM=`ps -fe| grep $SQLR_SCALER | grep -v grep | wc -l`
    if [ $SQLR_SCALER_NUM -eq 0 ];then
        STATUS="\033[31m[FAILED]\033[0m"
    else
        STATUS="\033[32m[SUCCESS]\033[0m"
    fi
    echo -e "$SQLR_SCALER \t\t $SQLR_SCALER_NUM   $STATUS"

    DATASYNC_NUM=`ps -fe| grep $DATASYNC | grep -v grep | wc -l`
    if [ $DATASYNC_NUM -eq 0 ];then
        STATUS="\033[31m[FAILED]\033[0m"
    else
        STATUS="\033[32m[SUCCESS]\033[0m"
    fi
    echo -e "$DATASYNC \t\t $DATASYNC_NUM   $STATUS"

}

start () {
    #延时2s, 保证sqlr进程全部已经退出了
    sleep 2s

    ps -fe|grep $SQLR_TOTAL |grep -v grep > /dev/null
    if [ $? -eq 0 ]; then
        echo "DBS is running.You can run 'systemctl restart dbs.service' to restart DBS"
        exit 1
    fi
    ps -fe|grep $DATASYNC |grep -v grep > /dev/null
    if [ $? -eq 0 ]; then
        echo "DBS is running.You can run 'systemctl restart dbs.service' to restart DBS"
        exit 1
    fi

    #确保资源清理完全
    ipc_clean

	LD_PRELOAD=$LOCKET_DIR/dbs/lib/libcrypto.so:$LOCKET_DIR/dbs/lib/libzlog.so  $SQLR_START -config $LOCKET_DIR/etc/sqlrelay.conf.d/dbs.conf -id mysql

    #延时启动
    sleep 3s

    LD_LIBRARY_PATH=$LOCKET_DIR/dbs/lib $DATASYNC &
}

stop () {
	$SQLR_STOP -id mysql 2> /dev/null
    killall -15 $DATASYNC 2> /dev/null

    #部分情况下，出现sqlrelay无法stop的情况，这时候用killall确保进程已杀死
    killall -15 $SQLR_LISTENER 2>/dev/null
    killall -15 $SQLR_CONNECTION 2>/dev/null
    killall -15 $SQLR_SCALER 2>/dev/null

    #确保资源清理完全
    ipc_clean
}

usage () {
	echo "Usage: $0 status|start|stop|restart"
	exit 1
}

checkuser () {
	if [ `whoami` != 'root' ]; then
	    echo "Please run as sudo command or root user"
	    exit
	fi
}

checkuser 

if [ $# -lt 1 ] 
then
	usage
fi


if [ "$1" == "status" ]
then
    status
elif [ "$1" == "start" ]
then
	start
elif [ "$1" == "stop" ]
then
	stop
elif [ "$1" == "restart" ]
then
	stop
	start
else
	usage
fi
